<!-- 角色的菜单权限 -->
<template>
  <div>
    <Modal v-model="modal.show"  :loading="modal.loading" :title="modal.title"  :mask-closable="false" :footer-hide=true width="700px" height="600px" :closable="false">
      <Tree ref="authTree" :data="authData" show-checkbox @on-check-change="change"></Tree>
      <Form>
        <FormItem>
          <div style="float: right;">
            <Button @click="close">{{$t('close')}}</Button>
            <Button @click="getNode" :disabled="!(canSave && changed)">{{$t('save')}}</Button>
          </div>
        </FormItem>
      </Form>
    </Modal>
  </div>
</template>
<script>
import TopnavPage from "_c/topnav/TopnavPage";
import RoleAuthorization from '@/api/RoleAuthorization';
import {apiResult} from "@/libs/util";
export default {
  name: "RoleAuthorization",
  components:{TopnavPage},
  data (){
    return{
      api: new RoleAuthorization(),
      modal:{
        show: false,
        loading: false,
        title: null
      },
      roleId: null,
      canSave: false,
      changed: false,
      authData: [],
      queryParam: {
        page: {
          current: 1,
          maxLimit: 10000,
          optimizeCountSql: true,
          pages: 0,
          searchCount: true,
          size: 10,
          total: 0
        },
        where:{}
      }
    }
  },
  methods:{
    getNode(){
      let auth=this.$refs.authTree.getCheckedAndIndeterminateNodes();
      if (auth.length === 0){
        this.api.roleAuthRemove(JSON.stringify({roleId:this.roleId})).then(res=>{
          apiResult(res.data,true,result=>{
            this.close();
          })
        })

      }else if (auth.length > 0){
        let params=[];
        auth.forEach(item=>{
          params.push({roleId:this.roleId,authId:item.id});
        });

        this.api.roleAuthBatchUpdate(JSON.stringify(params)).then(res=>{
          apiResult(res.data,true,result=>{
            this.close();
          });
        })
      }

      //console.log(this.$refs.authTree.getSelectedNodes());
      //console.log(this.$refs.authTree.getCheckedNodes());
    },
    getData(){
      /** 菜单 */
      this.api.queryDepthChildrens().then(res => {
          apiResult(res.data,false,result=>{
            let allData = result.data;
            this.api.queryByRole(JSON.stringify({roleId:this.roleId})).then(res1=>{
              apiResult(res1.data,false,result1=>{
                this.authData = this.resoleData(allData,result1.data);
              })
            });

          });
      });
    },
    resoleData(data,selData){
      if (!!data && data.length>0){
        data.forEach(item=>{
          //console.log("item=%o",item)
          //item["title"]= item.label;
          item["title"]= this.$t("p_"+item.arrt.code);
          item["expand"]=false;
          selData.forEach(item1=>{
            if (item1.authId == item.id){
              if (!item.children || item.children.length === 0){
                //console.log(item);
                item["checked"]=true;
              }
            }
          })
          if (!!item.children && item.children.length> 0){
             this.resoleData(item.children,selData);
          }
        })
      }
      return data;
    },
    openDialog(roleId,action){
      //console.log("roleId=%o,action=%o",roleId,action)
      this.modal.show = true;
      this.roleId = roleId;
      this.canSave = action==='EDIT';

      this.getData();
      this.changed =false;
    },
    close(){
      this.modal.show = false;
    },
    change(){
      //this.$Message.info("数据有改变");
      this.changed =true;
    }

  },
  mounted (){
    this.modal.title=this.$t('grant_permissions_for_role');//授予角色权限
  },
}
</script>
